<template>
  <el-dialog :title="`${form.bsNodeId ? '修改' : '添加'}检查节点`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="bsNodeId" class="hide" />
      <el-form-item label="检查类型" prop="businessType">
        <el-input v-model.trim="form.businessType" placeholder="请输入检查类型名称" :maxlength="20" clearable class="input-item" />
      </el-form-item>
      <el-form-item label="检查单位" prop="rdcType">
        <el-select v-model="form.rdcType" placeholder="请选择检查单位" clearable class="input-item">
          <el-option v-for="dict in dict.type.cq_random_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属系统" prop="belongSystem">
        <el-select v-model="form.belongSystem" placeholder="请选择所属系统" clearable class="input-item">
          <el-option v-for="dict in dict.type.cq_pe_belong_system" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="进入条件" prop="entryCondition">
        <el-select v-model="form.entryCondition" placeholder="请选择进入条件" filterable clearable class="input-item">
          <el-option v-for="dict in funcDictList" :key="dict.dictCode" :label="dict.dictName" :value="dict.dictCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="退出条件" prop="exitCondition">
        <el-select v-model="form.exitCondition" placeholder="请选择退出条件" filterable clearable class="input-item">
          <el-option v-for="dict in funcDictList" :key="dict.dictCode" :label="dict.dictName" :value="dict.dictCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="频次" prop="frequency">
        <div class="frequency-wrap input-item">
          <el-input-number v-model="form.frequency" :max="1000" controls-position="right" :min="1" style="width: 140px;" />
          <span>次</span>
          <span style="font-size: 18px;">/</span>
          <el-select v-model="form.frequencyUnit" placeholder="请选择频次单位" clearable style="width:120px;">
            <el-option v-for="dict in dict.type.cq_pe_frequency_unit" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="检查维度" prop="dimension">
        <el-select v-model="form.dimension" placeholder="请选择检查维度" clearable class="input-item">
          <el-option v-for="dict in dict.type.cq_pe_dimension" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="检查方式" prop="generationType">
        <el-select v-model="form.generationType" placeholder="请选择检查方式" clearable class="input-item">
          <el-option v-for="dict in dict.type.cq_pe_generation_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="考核方式" prop="dealType">
        <el-select v-model="form.dealType" placeholder="请选择考核方式" clearable class="input-item">
          <el-option v-for="dict in dict.type.cq_pe_ass_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="seq">
        <el-input-number size="small" v-model="form.seq" :min="0" class="input-item" />
      </el-form-item>
      <el-form-item label="是否过滤复杂地质" prop="filterComplex">
        <el-switch class="g-switch-in" v-model="form.filterComplex" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" :width="50" />
      </el-form-item>
    </el-form>
    <div slot="footer" style="margin-right: 40px;">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改检查节点
import { editFormMixin } from '@/utils/mixin'
import { addBsNode, updateBsNode } from '@/api/cqPe/bsNode'
import { listFuncDict } from '@/api/cqPe/funcDict'
export default {
  mixins: [editFormMixin],
  dicts: ['cq_random_type', 'cq_pe_frequency_unit', 'cq_pe_belong_system', 'cq_pe_generation_type', 'cq_pe_dimension', 'cq_pe_ass_type'],
  data() {
    return {
      primaryKey: 'bsNodeId',
      addApi: addBsNode,
      updateApi: updateBsNode,
      form: {
        bsNodeId: '',
        rdcType: '',
        businessType: '',
        belongSystem: '',
        entryCondition: '',
        exitCondition: '',
        frequency: '',
        frequencyUnit: 'YEAR',
        dimension: '',
        isEnable: true,
        generationType: '',
        dealType: '',
        filterComplex: 0,
        seq: 0
      },
      funcDictList: [],
      rules: {
        rdcType: [{ required: true, message: '请选择检查单位', trigger: 'change' }],
        businessType: [{ required: true, message: '请输入检查类型名称', trigger: 'blur' }],
        belongSystem: [{ required: true, message: '请选择所属系统', trigger: 'change' }],
        // entryCondition: [{ required: true, message: '请选择进入条件', trigger: 'change' }],
        // exitCondition: [{ required: true, message: '请选择退出条件', trigger: 'change' }],
        frequency: [{ required: true, message: '请选择周期', trigger: 'change' }],
        dimension: [{ required: true, message: '请选择检查维度', trigger: 'change' }],
        generationType: [{ required: true, message: '请选择检查方式', trigger: 'change' }],
        dealType: [{ required: true, message: '请选择考核方式', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getFuncDictList()
  },
  methods: {
    opened(item) {
      if (item?.frequencyUnit === null) this.form.frequencyUnit = 'EMPTY'
    },
    // 获取方法字典
    async getFuncDictList() {
      const res = await listFuncDict({pageNum: 1, pageSize: 1000}).catch(() => false)
      this.funcDictList = res.rows
    }
  }
}
</script>
<style scoped lang="scss">
  .input-item {
    width: 300px;
  }
  .frequency-wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>
